<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


class DisenoController extends Controller
{
    public function actionIndex()
    {
        // renders the view file 'protected/views/diseno/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->pageTitle = "SOSI - Interfaz de Diseño de la Encuesta";
        $dataProvider = new CActiveDataProvider('Encuesta');
        $this->render('index',array(
                'dataProvider'=>$dataProvider,
        ));
    }
    public function actionView($id){
        $this->render('view',array(
                'model'=>$this->loadModel($id),
            ));
    }
    
    public function loadModel($id)
    {
        $model=Encuesta::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }
    
    public function actionCreate(){
            $model=new Encuesta;

            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);
            
            if(isset($_POST['Encuesta']))
            {
                $_POST['Encuesta']['Usuarios_idUsuarios'] = Yii::app()->user->id;
                
                $model->attributes=$_POST['Encuesta'];
                if($model->save())
                    $this->redirect(array('edit','id'=>$model->idEncuesta));
            }

            $this->render('create',array(
                    'model'=>$model,
            ));
    }
    
    public function actionUpdate($id){
            $model=$this->loadModel($id);

            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);

            if(isset($_POST['Encuesta']))
            {
                    $model->attributes=$_POST['Encuesta'];
                    if($model->save())
                            $this->redirect(array('edit','id'=>$model->idEncuesta));
            }

            $this->render('update',array(
                    'model'=>$model,
            ));
    }
    
    public function actionDelete($id)
    {
            if(Yii::app()->request->isPostRequest)
            {
                    // we only allow deletion via POST request
                    $this->loadModel($id)->delete();

                    // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                    if(!isset($_GET['ajax']))
                            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
            }
            else
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }
    
    public function actionEdit($id){
        $this->render('edit',array(
                'model'=>$this->loadModel($id),
            ));
    }
    
    public function actionAddq($idE){
        //$this->render('editq');
        $model= new Preguntas;

        if(isset($_POST['Preguntas']))
        {
            $_POST['Preguntas']['Encuesta_idEncuesta'] = $idE;
            $_POST['Preguntas']['numeroPregunta'] = 0;
            $model->attributes=$_POST['Preguntas'];
            if($model->save())
                    $this->redirect(array('editq','id'=>$model->idPregunta ,'idE'=>$idE));
        }

        $this->render('createq',array(
                'model'=>$model,
                'idE'=>$idE,
        ));
    }
    
    public function actionEditq($id, $idE){
        $model=Preguntas::model()->findByPk($id);
        $this->render('editq',array(
                'model'=>$model,
                'idE'=>$idE,
            ));
    }
    
    public function actionUpdateq($id, $idE){
            $model=Preguntas::model()->findByPk($id);

            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);

            if(isset($_POST['Preguntas']))
            {
                    $model->attributes=$_POST['Preguntas'];
                    if($model->save())
                            $this->redirect(array('editq','id'=>$model->idPregunta ,'idE'=>$idE));
            }

            $this->render('updateq',array(
                    'model'=>$model,
                    'idE'=>$idE,
            ));
    }
    
    public function actionDeleteq($id){
        echo "Borrar pregunta";
    }
    
    public function actionAddqar($idQ, $idE){
        
        $model= new Argumentos;

        if(isset($_POST['Argumentos']))
        {
            $_POST['Argumentos']['Preguntas_idPregunta'] = $idQ;
            $model->attributes=$_POST['Argumentos'];
            if($model->save())
                    $this->redirect(array('editqar', 'id'=>$model->idArgumentos,'idQ'=>$idQ ,'idE'=>$idE));
        }

        $this->render('createqar',array(
                'model'=>$model,
                'idQ'=>$idQ,
                'idE'=>$idE,
        ));
    }
    
    public function actionEditqar($id,$idQ, $idE){
        $model= Argumentos::model()->findByPk($id);
        $this->render('editqar',array(
                'model'=>$model,
                'idQ'=>$idQ,
                'idE'=>$idE,
            ));
    }
    
    public function actionUpdateqar($id, $idQ, $idE){
            $model=  Argumentos::model()->findByPk($id);

            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);

            if(isset($_POST['Argumentos']))
            {
                    $model->attributes=$_POST['Argumentos'];
                    if($model->save())
                            $this->redirect(array('editqar','id'=>$model->idArgumentos,'idQ'=>$idQ ,'idE'=>$idE));
            }

            $this->render('updateqar',array(
                    'model'=>$model,
                    'idQ'=>$idQ,
                    'idE'=>$idE,
            ));
    }
    
    public function actionDeleteqar($id){
        echo "Borrar pregunta";
    }
}

?>
